/**
 * 角色管理模块相关的接口定义
 */
import request from '@/utils/request'
import {
  RoleData,
  RoleListResponseData,
  RolePermissionResponseData,
} from '@/api/acl/role/type'

/**
 * 定义相关的接口地址
 */
enum API {
  // 获取角色列表
  GET_ROLE_LIST_URL = '/admin/acl/role/',
  // 删除角色
  DELETE_ROLE_URL = '/admin/acl/role/remove/',
  // 添加角色
  ADD_ROLE_URL = '/admin/acl/role/save',
  // 修改角色
  UPDATE_ROLE_URL = '/admin/acl/role/update',
  // 根据角色获取权限菜单
  GET_PERMISSION_URL = '/admin/acl/permission/toAssign/',
  // 给角色分配权限菜单
  ASSIGN_PERMISSION_URL = '/admin/acl/permission/doAssign/?',
}

/**
 * 获取角色列表
 */
export const reqGetRoleList = (
  page: number | string,
  limit: number | string,
  roleName: string,
) => {
  return request.get<any, RoleListResponseData>(
    API.GET_ROLE_LIST_URL + `${page}/${limit}?roleName=${roleName}`,
  )
}

/**
 * 删除角色
 */
export const reqDeleteRole = (roleId: number | string) => {
  return request.delete<any, any>(API.DELETE_ROLE_URL + roleId)
}

/**
 * 添加或修改角色
 */
export const reqAddOrUpdateRole = (data: RoleData) => {
  if (data.id) {
    return request.put<any, any>(API.UPDATE_ROLE_URL, data)
  } else {
    return request.post<any, any>(API.ADD_ROLE_URL, data)
  }
}

/**
 * 根据角色获取权限菜单
 */
export const reqGetPermission = (roleId: number | string) => {
  return request.get<any, RolePermissionResponseData>(
    API.GET_PERMISSION_URL + roleId,
  )
}

/**
 * 给角色分配权限菜单
 */
export const reqAssignPermission = (
  roleId: number | string,
  permissionId: number[] | string[],
) => {
  return request.post<any, any>(
    API.ASSIGN_PERMISSION_URL + `roleId=${roleId}&permissionId=${permissionId}`,
  )
}
